<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue';
import type { FormInstance, FormRules } from 'element-plus'
import { Lock } from '@element-plus/icons-vue';
import { userUpdateService } from '@/api/user';
import { clearForm } from '@/utils/utils'
import { useUserStore } from '@/stores';
import router from '@/router'
import { userDeleteService } from '@/api/user'

const userStore = useUserStore()
const ruleForm = reactive({
    username: '',
    password: '',
    repassword: ''
})

const user = ref()
onMounted(() => {
    user.value = userStore.user
    ruleForm.username = user.value.username;
})
const ruleRef = ref<FormInstance>()
const validateRePass = (rule: any, value: any, callback: any) => {
    if (!ruleForm.password) return
    if (!value) {
        callback(new Error('请再次输入密码'))
    } else if (value !== ruleForm.password) {
        callback(new Error('两次输入密码不一致'))
    } else {
        callback()
    }
}
const rules = reactive<FormRules<typeof ruleForm>>({
    password: [
        { message: '请输入密码', trigger: 'blur' },
        {
            pattern: /^\S{6,15}$/,
            message: '密码必须是 6-15位 的非空字符',
            trigger: 'blur'
        }
    ],
    repassword: [{ validator: validateRePass, trigger: 'blur' }]
})

const updateInfo = async (form: FormInstance | undefined) => {
    if (!form) return
    await form.validate(async (valid) => {
        if (valid) {
            const { data } = await userUpdateService(ruleForm)
            ElMessage.success(data.message)
            clearForm(ruleForm)
        } else {
            ElMessage.warning('请检查输入')
        }
    })
}
const delUser = async () => {
    ElMessageBox.confirm(
        '确认删除账号吗？',
        'Warning',
        {
            type: 'warning',
            confirmButtonText: '确定',
            cancelButtonText: '取消'
        })
        .then(async () => {
            const { data } = await userDeleteService(ruleForm.username)
            ElMessage.success(data.message)
            userStore.setUser({})
            router.push('/login')
        })
        .catch()

}
</script>
<template>
    <el-card style="max-width: 480px">
        <template #header>
            <div class="card-header">
                <el-icon>
                    <Lock />
                </el-icon>
                <span>账户安全</span>
            </div>
        </template>
        <el-form ref="ruleRef" :model="ruleForm" :rules="rules">
            <el-form-item prop="password">
                <template #label>
                    <div class="input-label"><el-text type="warning">密码：</el-text></div>
                </template>
                <el-input v-model="ruleForm.password" :prefix-icon="Lock" type="password" placeholder="请输入密码" />
            </el-form-item>
            <el-form-item prop="repassword">
                <template #label>
                    <div class="input-label"><el-text type="warning">再次输入密码：</el-text></div>
                </template>
                <el-input v-model="ruleForm.repassword" :prefix-icon="Lock" type="password" placeholder="请再次输入密码" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button type="success" @click="updateInfo(ruleRef)">更新密码</el-button>
            <el-button type="danger" @click="delUser">删除账号</el-button>
        </template>
    </el-card>
</template>
<style lang="scss">
.el-card {
    min-width: 60%;
    margin-left: auto;
    margin-right: auto;

    .card-header {
        font-size: 20px;

        .el-icon {
            margin-right: 15px;
            font-size: 24px
        }
    }

    .el-form {
        .el-form-item {
            font-size: 16px;

            .input-label {
                width: 120px;
            }
        }
    }

    .el-button {
        height: 40px;
        width: 150px;
        font-size: 16px;
    }
}
</style>